/*
 * JaLingo, http://jalingo.sourceforge.net/
 *
 * Copyright (c) 2002-2006 Oleksandr Shyshko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uwncorrector.client.application.gui.main.settings.appearance;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import info.clearthought.layout.TableLayout;
import uwncorrector.centre.gui.actionbinder.ActionBinder;
import uwncorrector.centre.gui.util.BaseGui;
import uwncorrector.client.application.model.Model;
import uwncorrector.client.application.model.ModelAdapter;
import uwncorrector.client.application.model.Preferences;
import uwncorrector.client.application.util.Gaps;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.*;
import java.awt.*;

public class AppearanceGui extends BaseGui {
    private static final Log LOG = LogFactory.getLog( AppearanceGui.class );

    private static final int DEFAULT_FONT_SIZE = 14;

    private JPanel gui;

    private JCheckBox dropZoneCheckBox;
    private JComboBox fontSizeComboBox;
    private JCheckBox memoryBarCheckBox;
    private JTextField tfWelcomeLabel;

    public AppearanceGui(Model model) {
        model.addApplicationModelListener(new ModelAdapter() {
            public void settingsUpdated(Preferences preferences) {
                setSelectedFontSize(preferences.getFontSize());
                tfWelcomeLabel.setText(preferences.getWelcomeLabel());
                dropZoneCheckBox.setSelected(preferences.isDropZoneVisible());
                memoryBarCheckBox.setSelected(preferences.isMemoryBarVisible());
            }
        });

        dropZoneCheckBox  = new JCheckBox(resources.text("showDropZone"));
        fontSizeComboBox  = new JComboBox(new Integer[] { 12, 13, DEFAULT_FONT_SIZE, 15, 16, 17 });
        memoryBarCheckBox = new JCheckBox(resources.text("showMemoryBar"));
        JLabel lblForWelcome = new JLabel();
        lblForWelcome.setText(resources.text("welcome_label"));
        tfWelcomeLabel = new JTextField();
        JPanel welcomePanel = new JPanel(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        welcomePanel.add(lblForWelcome, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        welcomePanel.add(tfWelcomeLabel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));

        JPanel fontSizePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, Gaps.GAP5, 0));
        fontSizePanel.add(resources.label("fontSize"));
        fontSizePanel.add(fontSizeComboBox);
        fontSizePanel.add(resources.label("requiresRestart"));

        gui = new JPanel( new TableLayout( new double[][] {
                { TableLayout.PREFERRED },
                {
                        TableLayout.PREFERRED,  // 0: font
                        Gaps.GAP5,
                        TableLayout.PREFERRED,  // 2: memory bar
                        Gaps.GAP5,
                        TableLayout.PREFERRED,  // 4: auto-login panel
                }
        } ) );



        gui.add(fontSizePanel, "0, 0");
        gui.add(memoryBarCheckBox, "0, 2");
        gui.add(welcomePanel, "0, 4" );

        Gaps.applyBorder5(gui);

        ActionBinder.bind(this);
    }

    public JComponent getGui() {
        return gui;
    }

    private void setSelectedFontSize( int fontSize ) {
        ComboBoxModel model = fontSizeComboBox.getModel();
        for ( int i = 0; i < model.getSize(); i++ ) {
            Integer value = (Integer) model.getElementAt( i );
            if ( value.equals( fontSize ) ) {
                fontSizeComboBox.setSelectedItem( value );
                return;
            }
        }
        // TODO refactoring needed: move out default size + allowed sizes
        LOG.warn( "Incorrect font size: " + fontSize + ", setting size to default" );
        setSelectedFontSize( DEFAULT_FONT_SIZE ); // NOTE potential recusrion appearance on refactoring
    }

    public int getFontSize() {
        return (Integer) fontSizeComboBox.getSelectedItem();
    }
    public String getWelcomeLabel() {
        return tfWelcomeLabel.getText();
    }
    public boolean isDropZoneVisible() {
        return dropZoneCheckBox.isSelected();
    }
    public boolean isMemoryBarVisible() {
        return memoryBarCheckBox.isSelected();
    }
}